package utils

import (
	crand "crypto/rand"
	"encoding/hex"
	"math/rand"
	"time"
)

var mrand *rand.Rand = rand.New(rand.NewSource(time.Now().UnixNano()))

func NonceRaw(length int) (buffer []byte) {
	buffer = make([]byte, length)
	NonceBuffer(buffer)
	return
}

func NonceBuffer(buf []byte) []byte {
	if _, err := crand.Read(buf); nil != err {
		panic(err)
	}
	return buf
}

func Nonce(length int) string {
	return NonceHex(length)
}

func NonceHex(length int) string {
	return hex.EncodeToString(NonceRaw(length))
}

func NonceHexEx(length int, args ...interface{}) string {
	return HexEncoder(NonceRaw(length), args...)
}

func NonceInt() int64 {
	return int64(mrand.Int31())
}

func GetRandomString(l int) string {
	str := "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	bytes := []byte(str)
	result := []byte{}
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	for i := 0; i < l; i++ {
		result = append(result, bytes[r.Intn(len(bytes))])
	}
	return string(result)
}

func GetRandomStringFromNumber(l int) string {
	str := "0123456789"
	bytes := []byte(str)
	result := []byte{}
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	for i := 0; i < l; i++ {
		result = append(result, bytes[r.Intn(len(bytes))])
	}
	return string(result)
}
